<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { useCurrentUserStore } from '@/stores/useCurrentUserStore.ts' // 确保你的 Pinia store 路径正确
import { ElMessage } from 'element-plus' // 假设你已安装 ElMessage
import { Search } from '@element-plus/icons-vue' // 只需导入 Search 图标

// 获取 Pinia store 实例
const currentUserStore = useCurrentUserStore()
const router = useRouter()

// 计算属性：判断用户是否登录
const isLoggedIn = computed(() => currentUserStore.isLoggedIn)


// 计算属性：获取用户头像 URL，提供默认值
const userAvatar = computed(
  () =>
    currentUserStore.loginUser.infor.avatar ||
    'https://cube.elemecdn.com/0/88/03b0dff33e85e510429d2f07d57f1png.png',
)
// 计算属性：获取用户昵称，提供默认值
const userNickname = computed(() => currentUserStore.loginUser.infor.username || '用户')


// 导航栏主题 (如果你有主题切换功能的话)
const theme = ref('light') // 示例

// 登出操作
const handleLogout = () => {
  currentUserStore.userLogout()
  ElMessage.success('您已成功退出登录。') // 弹出成功提示
  router.push('/') // 登出后重定向到首页
}

// 搜索按钮点击事件 (模拟)
const handleSearchClick = () => {
  ElMessage.info('搜索功能正在开发中...')
}

// 点击头像或昵称跳转到个人中心
const goToProfile = () => {
  router.push('/auth/user-profile')
}
</script>

<template>
  <div id="top-header" :data-theme="theme">
    <el-row :gutter="10" justify="space-between" align="middle" class="header-row">
      <el-col :span="3" class="logo-col">
        <router-link to="/main" class="logo-text">TravelAid</router-link>
      </el-col>

      <el-col :span="10" class="main-nav-links">
        <router-link to="/main" class="nav-item">首页</router-link>
        <router-link to="/attractions" class="nav-item">攻略·景点</router-link>
        <router-link to="/map" class="nav-item">旅游地图</router-link>
        <router-link to="/red-education" class="nav-item">红色教育</router-link>
        <router-link to="/forum" class="nav-item">游记论坛</router-link>
      </el-col>

      <el-col :span="11" class="right-actions">
        <el-button size="default" :icon="Search" round @click="handleSearchClick">搜索</el-button>

        <template v-if="!isLoggedIn">
          <router-link to="/user-login" class="login-btn item">登录</router-link>
          <router-link to="/user-register" class="register-btn item">注册</router-link>
        </template>
        <template v-else>
          <el-popover placement="bottom" :width="180" popper-class="user-info-popover">
            <template #reference>
              <div class="popover-trigger-area">
                <el-avatar :size="40" :src="userAvatar" class="user-avatar-in-header"></el-avatar>
                <router-link to="/auth/user-profile" class="item">
                  <span class="user-nickname-in-header item">{{ userNickname }}</span>
                </router-link>
              </div>
            </template>
            <div class="popover-content-wrapper">
              <div class="popover-user-summary" @click="goToProfile">
                <el-avatar :size="50" :src="userAvatar"></el-avatar>
                <div class="popover-user-text">
                  <div class="popover-nickname">{{ userNickname }}</div>
                </div>
              </div>
              <el-divider class="popover-divider" />
              <el-button
                type="danger"
                size="small"
                round
                @click="handleLogout"
                class="popover-logout-btn"
                >退出登录
              </el-button>
            </div>
          </el-popover>
        </template>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
#top-header {
  height: 60px; /* 固定高度 */
  background-color: #ffffff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
  padding: 0 30px;
  display: flex;
  align-items: center;
  font-family: 'Arial', sans-serif;
  position: sticky; /* 粘性定位，使导航栏固定在顶部 */
  top: 0;
  z-index: 1000; /* 确保它在其他内容之上 */
  box-sizing: border-box; /* 包含 padding 和 border */
}

/* 主题变量 (示例) */
#top-header[data-theme='light'] {
  background-color: #ffffff;
  color: #333;
}

#top-header[data-theme='dark'] {
  background-color: #333;
  color: #eee;
}

.header-row {
  width: 100%;
  height: 100%; /* 确保 row 占据 header 的高度，方便 align-middle 居中 */
}

.logo-col {
  display: flex;
  align-items: center;
}

.logo-text {
  font-size: 24px;
  font-weight: bold;
  color: #007bff; /* 品牌色 */
  text-decoration: none;
}

.main-nav-links {
  display: flex;
  gap: 25px; /* 链接之间的间距 */
  flex-grow: 1; /* 占据剩余空间 */
  justify-content: flex-start;
  align-items: center; /* 垂直居中 */
}

.item {
  text-decoration: none;
}

/* --- 解决抖动问题的导航链接样式 --- */
.nav-item {
  color: #555;
  text-decoration: none;
  font-size: 16px;
  padding: 5px 0; /* 留出底部空间给伪元素 */
  position: relative; /* 为 ::after 伪元素提供定位基准 */
  transition: color 0.3s ease; /* 只过渡颜色，避免抖动 */
  line-height: 1.5; /* 确保文本行高一致，防止抖动 */
}

.nav-item::after {
  content: '';
  position: absolute;
  bottom: -2px; /* 调整下划线位置，使其在链接文字下方 */
  left: 50%; /* 从中间开始 */
  transform: translateX(-50%) scaleX(0); /* 初始不可见，从中间向两边展开 */
  width: 100%;
  height: 2px; /* 下划线高度 */
  background-color: #007bff; /* 下划线颜色 */
  transition: transform 0.3s ease-out; /* 平滑过渡动画 */
}

.nav-item:hover,
.nav-item.router-link-active {
  color: #007bff; /* 悬停和激活时改变颜色 */
}

.nav-item:hover::after,
.nav-item.router-link-active::after {
  transform: translateX(-50%) scaleX(1); /* 悬停和激活时展开下划线 */
}

/* --- 右侧功能区 --- */
.right-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end; /* 右对齐 */
  gap: 15px; /* 右侧元素间距 */
}

.login-btn,
.register-btn {
  padding: 8px 15px;
  border-radius: 20px;
  font-weight: bold;
  white-space: nowrap; /* 防止文字换行 */
  text-align: center;
}

.login-btn {
  background-color: #007bff;
  color: white;
  border: none;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #0056b3;
}

.register-btn {
  background-color: #e9ecef;
  color: #333;
  border: 1px solid #ddd;
  transition: background-color 0.3s;
}

.register-btn:hover {
  background-color: #dee2e6;
}

/* --- Popover 触发区域样式 --- */
.popover-trigger-area {
  display: flex;
  align-items: center;
  cursor: pointer;
  padding: 5px 10px; /* 增加点击区域 */
  border-radius: 25px; /* 圆角背景 */
  background-color: #f0f2f5;
  transition: background-color 0.3s;
}

.popover-trigger-area:hover {
  background-color: #e0e2e5; /* 悬停背景色 */
}

.user-avatar-in-header {
  margin-right: 8px; /* 头像与昵称间距 */
  border: 2px solid #fff; /* 给头像加个边框 */
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.1);
}

.user-nickname-in-header {
  font-weight: 500;
  color: #333;
  white-space: nowrap; /* 防止昵称换行 */
  overflow: hidden;
  text-overflow: ellipsis; /* 长昵称显示省略号 */
  max-width: 80px; /* 限制昵称宽度 */
}

/* --- Popover 内容样式 --- */
/* Element Plus 的 Popover 内容容器类名，可以覆盖其默认样式 */
:deep(.user-info-popover) {
  padding: 0 !important; /* 移除默认内边距 */
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15); /* 更强的阴影 */
  overflow: hidden; /* 确保圆角效果 */
  --el-popover-border-color: transparent !important; /* 隐藏 Popover 边框 */
}

.popover-content-wrapper {
  padding: 10px 0; /* 内部总内边距 */
}

.popover-user-summary {
  display: flex;
  align-items: center;
  padding: 10px 15px; /* 内部头像信息区域的内边距 */
  cursor: pointer;
  transition: background-color 0.2s;
}

.popover-user-summary:hover {
  background-color: #f5f7fa; /* 悬停背景色 */
}

.popover-user-summary .el-avatar {
  margin-right: 12px;
  border: 2px solid #fff;
  box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.1);
}

.popover-user-text {
  flex-grow: 1;
}

.popover-nickname {
  font-size: 16px;
  font-weight: bold;
  color: #333;
  margin-bottom: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.popover-level-tag {
  background-color: #007bff;
  color: white;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
  white-space: nowrap;
}

.popover-divider {
  margin: 10px 0 !important; /* 调整分割线间距 */
  border-color: #eee;
}

.popover-logout-btn {
  width: calc(100% - 30px); /* 按钮宽度，减去左右内边距 */
  margin: 0 15px 5px 15px; /* 上右下左内边距 */
}

/* --- 响应式设计 --- */
@media (max-width: 992px) {
  #top-header {
    padding: 0 15px;
  }

  .main-nav-links {
    gap: 15px;
  }

  .nav-item {
    font-size: 15px;
  }

  .logo-text {
    font-size: 20px;
  }

  .right-actions {
    gap: 10px;
  }

  .login-btn,
  .register-btn {
    padding: 6px 12px;
    font-size: 14px;
  }

  .user-nickname-in-header {
    max-width: 60px;
  }
}

@media (max-width: 768px) {
  .main-nav-links {
    display: none; /* 小屏幕下隐藏主导航链接 */
  }

  .el-col.main-nav-links {
    display: none;
  }

  .el-col.logo-col {
    flex: 0 0 auto;
    width: auto;
  }

  .right-actions {
    flex-grow: 1;
    justify-content: space-between;
  }

  .logo-text {
    margin-right: 0;
  }

  .popover-trigger-area {
    order: 1; /* 调整在小屏幕下的顺序 */
    margin-right: auto; /* 将头像推到左侧 */
  }

  .el-button.el-button--default.el-button--round {
    /* 针对搜索按钮 */
    order: 2; /* 调整搜索按钮顺序 */
  }
}

/* 确保 ElRow 和 ElCol 的 flex 行为 */
.el-row {
  height: 100%;
  align-items: center; /* 垂直居中所有列的内容 */
}

.el-col {
  display: flex;
  align-items: center;
}

.el-col:first-child {
  /* Logo 列 */
  justify-content: flex-start;
}

.el-col:last-child {
  /* 右侧功能列 */
  justify-content: flex-end;
}
</style>
